package com.shse.blog.web.controller;

import com.shse.blog.service.BlogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * Created by shse on 3/13/2016.
 */
@Controller
public class HomeController {

    @Autowired
    private BlogService blogService;

    //提供添加博文界面，对应/api/addBlog地址的get请求
    @RequestMapping(value = "/addBlog", method = RequestMethod.GET)
    public String addBlog(){
        return "addBlog";
    }

    //处理添加博文，对应/api/addBlog地址的post请求
    @RequestMapping(value = "/addBlog", method = RequestMethod.POST)
    public ResponseEntity<String> addBlog(@RequestParam("blogTitle") String blogTitle,
                                              @RequestParam("blogContent") String blogContent){

        //限制字符长度逻辑位于service层代码中，若超出，则返回值isGood为false
        Boolean isGood = blogService.addBlog(blogTitle, blogContent);
        if(!isGood){
            //返回400状态码，且一个自定义的简单响应体
            return new ResponseEntity<String>("HttpStatus Code: 400", HttpStatus.BAD_REQUEST);
        }else{
            //返回200状态码，且一个自定义的简单响应体
            return new ResponseEntity<String>("HttpStatus Code: 200", HttpStatus.OK);
        }
    }
}
